// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
require('es6-promise').polyfill()
import Vue from 'vue'
import app from './app.vue'
import router from './router'

Vue.config.productionTip = false

import Toast from './components/toast'

Vue.prototype.$message = Toast

import axios from 'axios'
import qs from 'qs'

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'

// 添加一个请求拦截器
axios.interceptors.request.use(function (config) {
  config.data = qs.stringify(config.data)
  return config
})

// 添加一个返回拦截器
axios.interceptors.response.use(function (response) {
  let data = response.data
  console.log(response.config.url, data)
  return data
}, function (error) {
  Toast('服务器正忙，请稍后重试!')
  return Promise.reject('服务器正忙!')
})

Vue.prototype.$http = axios

import VeeValidate, { Validator } from 'vee-validate'
import zh_CN from './assets/js/zh_CN'

Validator.extend('mobile', {
  messages: {
    zh_CN: field => '手机号码格式不正确',
  },
  validate: value => {
    return value.length == 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(value)
  },
})

Validator.addLocale(zh_CN)

const config = {
  errorBagName: 'errors',
  delay: 0,
  locale: 'zh_CN',
  messages: null,
  strict: true,
}
Vue.use(VeeValidate, config)

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<app/>',
  components: {app},
})
